package com.maddox.maddoxppjoke.util;

import android.content.res.AssetManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.maddox.libcommon.utils.AppGlobals;
import com.maddox.maddoxppjoke.model.BottomBar;
import com.maddox.maddoxppjoke.model.Destination;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * 用于读取本地配置文件的信息
 */
public class AppConfig {
    //页面导航信息
    private static HashMap<String, Destination> destConfig;
    //底部当行按钮
    private static BottomBar bottomBar;


    public static HashMap<String, Destination> getDestConfig() {
        if (destConfig == null) {
            String parseFile = parseFile("destination.json");
            destConfig = JSON.parseObject(parseFile, new TypeReference<HashMap<String, Destination>>() {
            }.getType());

        }
        return destConfig;
    }

    public static BottomBar getBottomBar() {
        if (bottomBar == null) {
            String content = parseFile("main_tabs_config.json");
            bottomBar = (BottomBar) JSON.parseObject(content, BottomBar.class);
        }
        return bottomBar;
    }

    private static String parseFile(String fileName) {
        AssetManager assetManager = AppGlobals.getApplication().getResources().getAssets();
        InputStream inputStream = null;
        BufferedReader reader = null;
        StringBuilder stringBuilder = null;
        try {
            inputStream = assetManager.open(fileName);
            reader = new BufferedReader(new InputStreamReader(inputStream));
            stringBuilder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }


        return stringBuilder.toString();
    }


}
